package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public void deleteWithDish(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> wrapper=new LambdaQueryWrapper();
        wrapper.in(Setmeal::getId,ids);
        wrapper.eq(Setmeal::getStatus,1);
        int count = this.count(wrapper);
        if (count>0){
            throw new CustomException("不能删除在售");
        }
        this.removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(queryWrapper);

    }

    @Override
    public void saveWithSetmealDto(SetmealDto setmealDto) {
        //先把setmeal中的基本数据添加进去
        this.save(setmealDto);
        //获取setmealdishs属性---数据对应的是setmealdish表
        Long id = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        /*LambdaQueryWrapper<SetmealDish> wrapper=new LambdaQueryWrapper();
        wrapper.eq(SetmealDish::getDishId,setmealDto.getId());*/
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }

        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public SetmealDto seleteSetmealDtoWithDish(Long id) {
        //先获取Setmeal表中数据
        Setmeal byId = this.getById(id);
        //把获取的数据拷贝到SetmealDto对象里
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(byId,setmealDto);
        //根据setmeal表中的id获取Setmeal_dish表中的数据封装到setmealDto中
        LambdaQueryWrapper<SetmealDish> wrapper=new LambdaQueryWrapper();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list=setmealDishService.list(wrapper);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    @Override
    public void updateDtoByIdWithDish(SetmealDto setmealDto) {
        //先把setmeal表的数据修改
        this.updateById(setmealDto);
        //删除setmeal_dish表中原来id相关的数据
        LambdaQueryWrapper<SetmealDish> wrapper=new LambdaQueryWrapper();
        wrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(wrapper);
        //获取setmeal_dish集合中的数据,添加进去
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }
}
